package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Category;
import com.example.springboot.entity.Item;
import com.example.springboot.entity.Poet;
import com.example.springboot.entity.Type;
import com.example.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IItemService itemService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private ITypeService typeService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IPoetService poetService;

    @GetMapping("/echarts")
    public Result echarts() {
        List<Type> types = typeService.list();
        List<String> x = new ArrayList<>();
        List<Long> y = new ArrayList<>();
        for (Type type : types) {
            x.add(type.getName());
            y.add(itemService.count(new LambdaQueryWrapper<Item>().eq(Item::getTypeId,type.getId())));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return Result.success(map);
    }

    @GetMapping("/echarts2")
    public Result echarts2() {
        List<Type> types = typeService.list();
        List<String> x = new ArrayList<>();
        List<Long> y = new ArrayList<>();
        for (Type type : types) {
            x.add(type.getName());
            y.add(itemService.count(new LambdaQueryWrapper<Item>().eq(Item::getTypeId,type.getId())));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return Result.success(map);
    }

    @GetMapping("/echarts3")
    public Result echarts3() {
        List<Type> types = typeService.list();
        List<Map<String, Object>> list = new ArrayList<>();
        for (Type type : types) {
            Map<String, Object> map = new HashMap<>();
            map.put("name",type.getName());
            map.put("value",itemService.count(new LambdaQueryWrapper<Item>().eq(Item::getTypeId,type.getId())));

            list.add(map);
        }
        return Result.success(list);
    }

    @GetMapping("/echarts4")
    public Result echarts4() {
        List<Category> types = categoryService.list();
        List<String> x = new ArrayList<>();
        List<Long> y = new ArrayList<>();
        for (Category type : types) {
            x.add(type.getName());
            y.add(poetService.count(new LambdaQueryWrapper<Poet>().eq(Poet::getType,type.getName())));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return Result.success(map);
    }

    @GetMapping("/echarts5")
    public Result echarts5() {
        List<Category> types = categoryService.list();
        List<String> x = new ArrayList<>();
        List<Long> y = new ArrayList<>();
        for (Category type : types) {
            x.add(type.getName());
            y.add(poetService.count(new LambdaQueryWrapper<Poet>().eq(Poet::getType,type.getName())));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return Result.success(map);
    }

    @GetMapping("/echarts6")
    public Result echarts6() {
        List<Category> types = categoryService.list();
        List<Map<String, Object>> list = new ArrayList<>();
        for (Category type : types) {
            Map<String, Object> map = new HashMap<>();
            map.put("name",type.getName());
            map.put("value",poetService.count(new LambdaQueryWrapper<Poet>().eq(Poet::getType,type.getName())));

            list.add(map);
        }
        return Result.success(list);
    }

    @GetMapping("/counts")
    public Result counts() {
        Map<String, Object> map = new HashMap<>();
        map.put("users",userService.count());
        map.put("items",itemService.count());
        map.put("messages",messageService.count());
        return Result.success(map);
    }
}
